//
//  SXTopicHeaderView.m
//  ShiXun
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXTopicHeaderView.h"
#import "UIView+UIView.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "SXCollectionManager.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"


@implementation SXTopicHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
               
        _avatar_urlImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        _avatar_urlImage.layer.masksToBounds = YES;
        _avatar_urlImage.layer.cornerRadius = 15;
        [self addSubview:_avatar_urlImage];
        
        _screen_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 10, kScreenWidth / 2, 30)];
        _screen_nameLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_screen_nameLabel];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, kScreenWidth - 20, 30)];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_contentLabel];
        
        _shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        UIImageView *imageS = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 30, 20)];
        imageS.image = [UIImage imageNamed:@"Topic-fenxiang"];
        [_shareButton addSubview:imageS];
        UILabel *labelL = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 50, 30)];
        labelL.text = @"转发";
        labelL.font = [UIFont systemFontOfSize:12];
        [_shareButton addSubview:labelL];
        [self addSubview:_shareButton];
        
        _collectionButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"Topic-shoucang1"] forState:UIControlStateNormal];
        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"SX-yuedushoucang1"] forState:UIControlStateSelected];
        [self addSubview:_collectionButton];
        
        _collectionB = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _collectionButton.selected = [SXCollectionManager checkCollectionWithTableName:@"topic" urlID:self.configureModel.thread_id];
        [_collectionB setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_collectionB setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected)];
        [_collectionB setTitle:@"收藏" forState:(UIControlStateNormal)];
        [_collectionB setTitle:@"取消收藏" forState:(UIControlStateSelected)];
        _collectionB.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_collectionB];
    }
    return self;
}
// 赋值
- (void)setConfigureModel:(SXTopicListModel *)configureModel
{
    _configureModel = configureModel;
    [_avatar_urlImage sd_setImageWithURL:[NSURL URLWithString:_configureModel.userModel.avatar_url]];
    _screen_nameLabel.text = _configureModel.userModel.screen_name;
    _contentLabel.text = _configureModel.content;
    _contentLabel.frame = CGRectMake(10, 50, kScreenWidth - 20, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text]);
    // 分享按钮
    _shareButton.frame = CGRectMake(10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 60, 90, 30);
    // 收藏按钮
    _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 63, 25, 25);
    _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 60, 60, 30);

    if (configureModel.thumbImageArray.count > 0) {
        if (configureModel.thumbImageArray.count == 1) {
            UIButton *thumbButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
            thumbButton.frame = CGRectMake(10 + 0 * ((kScreenWidth - 30) / 3 + 5) , [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 60, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3 + 30);
            // 图片加载
            [thumbButton sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[0] Imgurl]] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
            // 点击图片
            thumbButton.tag = 0 + 1500;
            [thumbButton addTarget:self action:@selector(doTapthumbButton:) forControlEvents:(UIControlEventTouchUpInside)];
            // 分享按钮
            _shareButton.frame = CGRectMake(10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 100 + (kScreenWidth - 30) / 3, 90, 30);
            // 收藏按钮
            _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 103 + (kScreenWidth - 30) / 3, 25, 25);
            _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 100 + (kScreenWidth - 30) / 3, 50, 30);
            [self addSubview: thumbButton];
        } else{
        for (int i = 0; i < configureModel.thumbImageArray.count; i++) {
            if (i < 3) {
                UIButton *thumbButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
                 thumbButton.frame = CGRectMake(10 + i * ((kScreenWidth - 30) / 3 + 5) , [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 60, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3);
                // 图片加载
                [thumbButton sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[i] Imgurl]] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbButton.tag = i + 1500;
                [thumbButton addTarget:self action:@selector(doTapthumbButton:) forControlEvents:(UIControlEventTouchUpInside)];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 70 + (kScreenWidth - 30) / 3, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 73 + (kScreenWidth - 30) / 3, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 70 + (kScreenWidth - 30) / 3, 50, 30);
                [self addSubview: thumbButton];
            }else if (i > 2 && i < 6) {
                UIButton *thumbButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
                thumbButton.frame = CGRectMake(10 + (i - 3) * ((kScreenWidth - 30) / 3 + 5) , [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 70 + (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3);
                // 图片加载
                [thumbButton sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[i] Imgurl]] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbButton.tag = i + 1500;
                [thumbButton addTarget:self action:@selector(doTapthumbButton:) forControlEvents:(UIControlEventTouchUpInside)];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 2, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 83 + (kScreenWidth - 30) / 3 * 2, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 2, 50, 30);
                [self addSubview: thumbButton];
            } else {
                UIButton *thumbButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
                thumbButton.frame = CGRectMake(10 + (i - 6) * ((kScreenWidth - 30) / 3 + 5) , [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 2, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3);
                // 图片加载
                [thumbButton sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[i] Imgurl]] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbButton.tag = i + 1500;
                [thumbButton addTarget:self action:@selector(doTapthumbButton:) forControlEvents:(UIControlEventTouchUpInside)];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 90 + (kScreenWidth - 30) / 3 * 3, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 93 + (kScreenWidth - 30) / 3 * 3, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicHeaderView heightHeaderOfContent:_contentLabel.text] + 90 + (kScreenWidth - 30) / 3 * 3, 50, 30);
                [self addSubview: thumbButton];
            }
        }
    }
}
    [self setNeedsDisplay];
}
// 点击图片放大
- (void)doTapthumbButton:(UIButton *)button
{
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:_configureModel.thumbImageArray.count];
    for (int i = 0; i < _configureModel.thumbImageArray.count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [[_configureModel.thumbImageArray[i] Imgurl] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        photo.srcImageView = (UIImageView*)[self.View viewWithTag:i + 1500]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = button.tag-1500; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}

// 自适应高度
+ (CGFloat)heightHeaderOfContent:(NSString *)content
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [content boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
